import Taro, { Component } from '@tarojs/taro'
import { View , Text , Image} from '@tarojs/components';
import { baseImgSrc } from '../../util/base_url';
import { AtButton } from 'taro-ui';
import { upDriverAuth , upServerFile } from '../../util/api';
import './idauthUser.scss';
export default class IdAuth extends Component{
    config = {
        navigationBarTitleText:"认证中心",
    }
    constructor(){
        super(...arguments);
        this.state  = {
            driverZpic:"",
            realDriverZpic:"",
            driverFpic:"",
            realDriverFpic:"",
            defaultPic:`${baseImgSrc}/updefault.png`,
        }
    }
    // 选择驾驶证正面
    choseDriveZ(){
        Taro.chooseImage({
           count:1,
        }).then( res => {
            let driverZpic = res.tempFilePaths[0];
            upServerFile(driverZpic).then( res => { this.setState({driverZpic,realDriverZpic:res.data,})})
        })
    }
    //选择驾驶证反面
    choseDriverF(){
        Taro.chooseImage({
            count:1,
        }).then( res => {
            let driverFpic = res.tempFilePaths[0];
            upServerFile(driverFpic).then( res => { this.setState({driverFpic,realDriverFpic:res.data,})})
        })
    }
    submitCheck(){
        let {  driverZpic , realDriverZpic , driverFpic , realDriverFpic } = this.state;
        if(!driverZpic){
            Taro.atMessage({
                message:"请上传身份证正面照",
                type:'error'
            })
            return false
        }
        if(!driverFpic){
            Taro.atMessage({
                message:"请上传身份证背面照",
                type:'error'
            })
            return false
        }
        Taro.showLoading({title:"正在提交公安部认证"})
        upDriverAuth({
            img_one:realDriverZpic,
            img_two:realDriverFpic,
        },true).then( res => {
            Taro.hideLoading();
            Taro.showModal({
                title:"提示",
                content:res.msg
            }).then( res => {
                Taro.navigateBack();
            })
        })
    }
    render(){
        let { driverZpic , driverFpic } = this.state; 
        return(
            <View className='auth'>
                <View className='up-box'>
                    <View className='title flex align-items flex-start'>请上传驾驶证正本</View>
                    <View className='pic-box' onClick={this.choseDriveZ.bind(this)}>
                        {
                            driverZpic == '' ?  <Image className='pic updefault' src={baseImgSrc + "/jiashi_zheng.png"} /> : <Image className='pic' src={driverZpic} />
                        }
                    </View>
                    <View className='title flex align-items flex-start'>请上传驾驶证副本</View>
                    <View className='pic-box' onClick={this.choseDriverF.bind(this)}>
                        {
                            driverFpic == '' ?  <Image className='pic updefault' src={baseImgSrc + "/jiashi_bei.png"} /> : <Image className='pic' src={driverFpic} />
                        }
                    </View>
                    <AtButton className='next-tip' type='primary' size='normal' onClick={this.submitCheck.bind(this)}>开始认证</AtButton>
                </View>
            </View>
        )
    }
}